<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends ZhiyeAdmin_Controller {

    public function __construct(){

        parent::__construct();

		$this->load->model('test_model','test');
		$this->load->library('curl');


    }

	public function index(){

		//$data['moudle_name']="初始模板";		
		$this->zhiyeAdmin_view("index", $data);		

		//echo json_encode($data);

    }
    public function layout_templet(){
		$data['moudle_name']="初始模板";
		$this->zhiyeAdmin_view("layout-templet", $data);		

		//echo json_encode($data);

	}

	public function add(){
        //获取系统值
        $this->load->model('system_setup_model','system_setup');
        $where=" where is_delete=0";
        $sql="SELECT * FROM `system_setup` ".$where;
        $data['system']=$this->system_setup->execute($sql,true,0);
        foreach (json_decode(json_encode($data['system'])) as $system )
        {
            $system_setup[$system->remarks.'_system']=$system->key_value;
        }
        $appid=$system_setup['appid_system'];
        $otp_code=$system_setup['otp_code_system'];
        
        $account1_id=26;
        $account2_id=27;
        $auto_trade['trade_way']='btcaud';
        $trade_num=1;
       
        $this->load->model('account_model','account');
        $this->load->model('balance_model','balance');
        
        // get account1 sessions   
        $data["account1"]=$this->account->select_one($account1_id);
        $account1_email=$data["account1"]["account"];
        $account1_password=$data["account1"]["account_password"];
        $url = $system_setup['sessions_url_system']."sessions?email=$account1_email&password=$account1_password&application_id=$appid&otp_code=$otp_code";
        $account1_sessions=$this->curl->ssl()->simple_post($url);
        $account1_sessions=str_replace("\"","",$account1_sessions);

        // get account2 sessions
        $data["account2"]=$this->account->select_one($account2_id);
        $account2_email=$data["account2"]["account"];
        $account2_password=$data["account2"]["account_password"];
        $url = $system_setup['sessions_url_system']."sessions?email=$account2_email&password=$account2_password&application_id=$appid&otp_code=$otp_code";
        $account2_sessions=$this->curl->ssl()->simple_post($url);
        $account2_sessions=str_replace("\"","",$account2_sessions);

        $headers1=array(
            "Authorization"=>" Bearer $account1_sessions",
        );
        $headers2=array(
            "Authorization"=>" Bearer $account2_sessions",
        );

        //get accounts1
        $url=$system_setup['api_url_system']."accounts";
		$accounts1=$this->curl->ssl()->http_header($headers1)->simple_get($url);   
       
        //get accounts2
        $url=$system_setup['api_url_system']."accounts";
		$accounts2=$this->curl->ssl()->http_header($headers2)->simple_get($url);  
        
        for($i=0;$i<1;$i++){
			$this->order_post($system_setup['api_url_system'],$auto_trade['trade_way'],$auto_trade['account1_id'],$auto_trade['account2_id'],$headers1,$headers2); 

            // $this->order_accounts1_sell_post($system_setup['api_url_system'],$auto_trade['trade_way'],$auto_trade['account1_id'],$auto_trade['account2_id'],$headers1,$headers2); 
			// $this->order_accounts2_buy_post($system_setup['api_url_system'],$auto_trade['trade_way'],$auto_trade['account1_id'],$auto_trade['account2_id'],$headers1,$headers2); 
            // $this->order_accounts2_sell_post($system_setup['api_url_system'],$auto_trade['trade_way'],$auto_trade['account1_id'],$auto_trade['account2_id'],$headers1,$headers2); 
            // $this->order_accounts1_buy_post($system_setup['api_url_system'],$auto_trade['trade_way'],$auto_trade['account1_id'],$auto_trade['account2_id'],$headers1,$headers2); 

		}
		$this->order_trades(); 

        die();
		//redirect('auto_trade/auto_trade/index');
	}
	public function order_trades(){
		//获取系统值
        $this->load->model('system_setup_model','system_setup');
        $where=" where is_delete=0";
        $sql="SELECT * FROM `system_setup` ".$where;
        $data['system']=$this->system_setup->execute($sql,true,0);
        foreach (json_decode(json_encode($data['system'])) as $system )
        {
            $system_setup[$system->remarks.'_system']=$system->key_value;
        }
        $appid=$system_setup['appid_system'];
        $otp_code=$system_setup['otp_code_system'];
		 // get account1 sessions   
		 $account1_email='test1@sccx.exchange';
		 $account1_password='Welcome111';
		 $url = $system_setup['sessions_url_system']."sessions?email=$account1_email&password=$account1_password&application_id=$appid&otp_code=$otp_code";
		 $account1_sessions=$this->curl->ssl()->simple_post($url);
		 $account1_sessions=str_replace("\"","",$account1_sessions);
 
		 $headers1=array(
			 "Authorization"=>" Bearer $account1_sessions",
		 );
		 echo json_encode($headers1)."<hr />";


		  // get account1 sessions   
		  $account2_email='test2@sccx.exchange';
		  $account2_password='Welcome222';
		  $url = $system_setup['sessions_url_system']."sessions?email=$account2_email&password=$account2_password&application_id=$appid&otp_code=$otp_code";
		  $account2_sessions=$this->curl->ssl()->simple_post($url);
		  $account2_sessions=str_replace("\"","",$account2_sessions);
  
		  $headers2=array(
			  "Authorization"=>" Bearer $account2_sessions",
		  );
		  echo json_encode($headers2)."<hr />";

		$this->load->model('orders_model','orders');
        $sql="SELECT a.id, a.order_id, a.account,b.account_password,a.side,a.remarks FROM `orders` a,account b WHERE a.remarks='[]' AND a.account=b.account ";
		$data['orders']=$this->orders->execute($sql,true,0);
		foreach (json_decode(json_encode($data['orders'])) as $order) {
			 //update order trades
			 $url=$system_setup['api_url_system']."order?id=".$order->order_id;
			 if($order->account=='test1@sccx.exchange'){
				$order_trades=$this->curl->ssl()->http_header($headers1)->simple_get($url);
				echo $order_trades."<hr />";
			 }else{
				$order_trades=$this->curl->ssl()->http_header($headers2)->simple_get($url);
				echo $order_trades."<hr />";
			 }
			 $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
			 echo $orders['remarks']."<hr />";

			 $i=$this->orders->update($orders, array('id='.$order->id)); 
		}
		die();
		
	}

	public function order_accounts1_sell_post($api_url_system,$market,$account1_id,$account2_id,$headers1,$headers2){
		//get order_book
		$url=$api_url_system."order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;
		$my_price=$buy+($sell-$buy)*$price_rand;
	    /*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account1 sell order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        $this->insert_account1_sell_orders($api_url_system,$create_sell_order,$headers1,$account1_id);		                                     		
	}
	public function order_accounts2_buy_post($api_url_system,$market,$account1_id,$account2_id,$headers1,$headers2){
		//get order_book
		$url=$api_url_system."order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;
		$my_price=$buy+($sell-$buy)*$price_rand;
	    /*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account2 buy order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
        $this->insert_account2_buy_orders($api_url_system,$create_buy_order,$headers2,$account2_id);		                                     
	
	}
	public function order_accounts2_sell_post($api_url_system,$market,$account1_id,$account2_id,$headers1,$headers2){
		//get order_book
		$url=$api_url_system."order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;
		$my_price=$buy+($sell-$buy)*$price_rand;
	    /*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account2 sell order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
        $this->insert_account2_sell_orders($api_url_system,$create_sell_order,$headers2,$account2_id);		                                     
	}
	public function order_accounts1_buy_post($api_url_system,$market,$account1_id,$account2_id,$headers1,$headers2){
		//get order_book
		$url=$api_url_system."order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;
		$my_price=$buy+($sell-$buy)*$price_rand;
	    /*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account1 buy order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
        $create_buy_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        $this->insert_account1_buy_orders($api_url_system,$create_buy_order,$headers1,$account1_id);		                                     

	}
    public function order_post($api_url_system,$market,$account1_id,$account2_id,$headers1,$headers2){
		//get order_book
		$url=$api_url_system."order_book?market=$market&bids_limit=1&asks_limit=1";
		$order_book=$this->curl->ssl()->http_header($headers2)->simple_get($url);
		$order_book=json_decode($order_book,false);
		//get the middle price
		$buy=$order_book->bids[0]->price;
		$sell=$order_book->asks[0]->price;
		$price_rand=mt_rand(1,9)/10;
		$my_price=$buy+($sell-$buy)*$price_rand;
	    /*
		如果没有订单得话 这个价格怎么定	
		if($my_price==0){
			$my_price=8872.5;
		}*/
		//数量不要超过1
		$volume=mt_rand(1,9)/10;
		//create account1 sell order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        $this->insert_account1_sell_orders($api_url_system,$create_sell_order,$headers1,$account1_id);		                                     

		//create account2 buy order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_buy_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
        $this->insert_account2_buy_orders($api_url_system,$create_buy_order,$headers2,$account2_id);		                                     

		//create account2 sell order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"sell",
			"volume"=>$volume,
			"price"=>$my_price
		);
		$create_sell_order=$this->curl->ssl()->http_header($headers2)->simple_post($url, $post_data);
        $this->insert_account2_sell_orders($api_url_system,$create_sell_order,$headers2,$account2_id);		                                     

		//create account1 buy order
		$url=$api_url_system."orders";
		$post_data=array(
			"market"=>$market,
			"side"=>"buy",
			"volume"=>$volume,
			"price"=>$my_price
		);
        $create_buy_order=$this->curl->ssl()->http_header($headers1)->simple_post($url, $post_data);
        $this->insert_account1_buy_orders($api_url_system,$create_buy_order,$headers1,$account1_id);		                                     

    }
    //,$headers,$account
    public function insert_account1_sell_orders($api_url_system,$create_order,$headers,$account_id){
        //update order trades
        $url=$api_url_system."order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		
		echo json_encode($headers)."<hr />";
		echo "====================================================================<br />";
		echo $order_trades."<hr />";
		echo "====================================================================<br />";
		echo $url."<hr />";
		echo "====================================================================<br />";    
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
		echo "备注remarks".$orders['remarks']."<hr />";
		echo "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<br />";  
		$this->test2(json_decode($create_order)->id);	
	}
	public function insert_account2_sell_orders($api_url_system,$create_order,$headers,$account_id){
        //update order trades
        $url=$api_url_system."order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		
		echo json_encode($headers)."<hr />";
		echo "====================================================================<br />";
		echo $order_trades."<hr />";
		echo "====================================================================<br />";
		echo $url."<hr />";
		echo "====================================================================<br />";    
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
		echo "备注remarks".$orders['remarks']."<hr />";
		echo "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<br />";  
	}
	public function insert_account1_buy_orders($api_url_system,$create_order,$headers,$account_id){
        //update order trades
        $url=$api_url_system."order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		
		echo json_encode($headers)."<hr />";
		echo "====================================================================<br />";
		echo $order_trades."<hr />";
		echo "====================================================================<br />";
		echo $url."<hr />";
		echo "====================================================================<br />";    
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
		echo "备注remarks".$orders['remarks']."<hr />";
		echo "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<br />";  
	}
	public function insert_account2_buy_orders($api_url_system,$create_order,$headers,$account_id){
        //update order trades
        $url=$api_url_system."order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		
		echo json_encode($headers)."<hr />";
		echo "====================================================================<br />";
		echo $order_trades."<hr />";
		echo "====================================================================<br />";
		echo $url."<hr />";
		echo "====================================================================<br />";    
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
		echo "备注remarks".$orders['remarks']."<hr />";
		echo "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<br />";  
	}
	public function updat_remarks_orders($api_url_system,$create_order,$headers,$account_id){
        //update order trades
        $url=$api_url_system."order?id=".json_decode($create_order)->id;
		$order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		
		echo json_encode($headers)."<hr />";
		echo "====================================================================<br />";
		echo $order_trades."<hr />";
		echo "====================================================================<br />";
		echo $url."<hr />";
		echo "====================================================================<br />";    
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
		echo "备注remarks".$orders['remarks']."<hr />";
		echo "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<br />";  
	}
	 //,$headers,$account
	 public function remarks_orders(){
        //update order trades
		$url="https://t.sccx.exchange/api/v2/order?id=35141";
		$headers=array(
            "Authorization"=>" Bearer eyJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1NTEzNDY1OTYsImV4cCI6MTU1MTM2MDk5Niwic3ViIjoic2Vzc2lvbiIsImlzcyI6ImJhcm9uZyIsImF1ZCI6WyJwZWF0aW8iLCJiYXJvbmciXSwianRpIjoiQzQxREVCQjFDNzBGMzAxN0Q5QzA1QTkxIiwidWlkIjoiSUQzQTZEMTFDQ0I4IiwiZW1haWwiOiJ0ZXN0MUBzY2N4LmV4Y2hhbmdlIiwicm9sZSI6Im1lbWJlciIsImxldmVsIjozLCJzdGF0ZSI6ImFjdGl2ZSJ9.qsnOSN0zUmW1ZUzJ-G7GRfgcMFeY7Gn0O-cggubvBTZVOgXyXtl3KfoT6QoC8KrbxW7NZ1515MEv6uqSDqcHhzmVRvTv8wik-Q4uJ8cG_rfvl7vaGPcxLTs-EC9Tkn_emBSFeoqM5A-YazqLTXtkIzJdXFblRaDXTgz7dR_bqBWAuUsnmtbcoSjKbWbsxWVLWgDu6ut9YBVHjB_LewwfykWgm9p2IqO2sSXuNij9mSRFQWGUDZxz4aUBXv6B9UUvBQoyI2ez10oz1K5_WuYKIhR-ksR1WO3vzWHzmZbMACFeBbaNz0LAmXiTQK7Memv9DsebpLfsyzhXqWDRmgEvCg",
        );
        echo $url."<hr />";
        $order_trades=$this->curl->ssl()->http_header($headers)->simple_get($url);
		echo $order_trades."<hr />";          
        $orders['remarks']=json_encode(json_decode($order_trades)->trades);   
        echo "备注remarks".$orders['remarks']."<hr />";
	}
	public function test2($id){
		//获取系统值
        $this->load->model('system_setup_model','system_setup');
        $where=" where is_delete=0";
        $sql="SELECT * FROM `system_setup` ".$where;
        $data['system']=$this->system_setup->execute($sql,true,0);
        foreach (json_decode(json_encode($data['system'])) as $system )
        {
            $system_setup[$system->remarks.'_system']=$system->key_value;
        }
        $appid=$system_setup['appid_system'];
        $otp_code=$system_setup['otp_code_system'];
        
        // get account1 sessions   
        $account1_email="test1@sccx.exchange";
        $account1_password="Welcome111";
        $url = $system_setup['sessions_url_system']."sessions?email=$account1_email&password=$account1_password&application_id=$appid&otp_code=$otp_code";
        $account1_sessions=$this->curl->ssl()->simple_post($url);
        $account1_sessions=str_replace("\"","",$account1_sessions);
		echo $account1_sessions."<hr />";  

		$headers=array(
            "Authorization"=>" Bearer $account1_sessions",
		);

		$arr=array("id"=>$id);
		$this->updat_remarks_orders($system_setup['api_url_system'],json_encode($arr),$headers,0);
	}
}